package com.net.shop.car.manager.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.shop.car.manager.R;
import com.net.shop.car.manager.utils.AnimUtils;
import com.net.shop.car.manager.utils.BaseUtils;

/**
 * @description 标题栏
 * @author wangpeng
 * @date 2014-12-20
 */
public class TitleView extends FrameLayout {

	private TextView txTitle;
	private LinearLayout layLeft, layRight;
	private TextView textRight;

	private static final long DEFAULT_ANIM_DURATION = 100;
	private static int menuId = Integer.MAX_VALUE / 2;

	public TitleView(Context context) {
		this(context, null);
	}

	public TitleView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TitleView(final Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		LayoutInflater.from(context).inflate(R.layout.view_title, this);
		txTitle = (TextView) findViewById(R.id.tx_title);
		layLeft = (LinearLayout) findViewById(R.id.layout_left);
		layRight = (LinearLayout) findViewById(R.id.layout_right);
		textRight = (TextView) findViewById(R.id.text_right);

		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
		String title = ta.getString(R.styleable.TitleView_yqtitle);
		txTitle.setText(title);
		ta.recycle();
	}

	public void setTitle(final int textId) {
		txTitle.setText(textId);
	}

	public void setTitle(final int textId, boolean useDefaultAnim) {
		if (useDefaultAnim) {
			long duration = DEFAULT_ANIM_DURATION;
			Animation exit = AnimUtils.init().addAlpha(1f, 0.4f, 0, duration, new LinearInterpolator(), 1)
					.addTranslate(0f, 30f, 0f, 0f, 0, duration, new LinearInterpolator()).create();
			Animation enter = AnimUtils.init().addAlpha(0.4f, 1f, 0, duration, new LinearInterpolator(), 1)
					.addTranslate(-30f, 0f, 0f, 0f, 0, duration, new LinearInterpolator()).create();
			setTitle(textId, exit, enter, duration);
		} else {
			setTitle(textId);
		}
	}

	public void setTitle(final int textId, final Animation exit, final Animation enter, final long duration) {
		txTitle.startAnimation(exit);
		txTitle.postDelayed(new Runnable() {
			@Override
			public void run() {
				txTitle.setText(textId);
				txTitle.clearAnimation();
				txTitle.startAnimation(enter);
			}
		}, duration);
	}

	public void setTitle(String text) {
		txTitle.setText(text);
	}

	public View addLeftDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
		return addLeftDrawableMenu(context, drawableId, width, height, onMenuClickListener, false);
	}

	public View addLeftDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener, boolean useDefaultAnim) {
		View view = createDrawableMenu(context, drawableId, width, height, onMenuClickListener);
		LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
		int left = BaseUtils.dip(getContext(), 10);
		int right = BaseUtils.dip(getContext(), 20);
		view.setPadding(left, 0, right, 0);
		view.setLayoutParams(LP);
		addViewToLeftLayout(view, useDefaultAnim);
		setIdTagForMenu(view);
		return view;
	}

	public View addLeftDrawableFinish(final Activity context) {
		return addLeftDrawableFinish(context, false);
	}

	public View addLeftDrawableFinish(final Activity context, boolean useDefaultAnim) {
		int size = BaseUtils.dip(context, 10);
		View view = createDrawableMenu(context, R.drawable.btn_back, size, size, new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				context.finish();
			}
		});
		LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
		int left = BaseUtils.dip(getContext(), 10);
		int right = BaseUtils.dip(getContext(), 20);
		view.setPadding(left, 0, right, 0);
		view.setLayoutParams(LP);
		addViewToLeftLayout(view, useDefaultAnim);
		setIdTagForMenu(view);
		return view;
	}

	public View addRightDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
		return addRightDrawableMenu(context, drawableId, width, height, onMenuClickListener, false);
	}

	public View addRightDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener, boolean useDefaultAnim) {
		View view = createDrawableMenu(context, drawableId, width, height, onMenuClickListener);
		LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
		int left = BaseUtils.dip(getContext(), 20);
		int right = BaseUtils.dip(getContext(), 10);
		view.setPadding(left, 0, right, 0);
		view.setLayoutParams(LP);
		addViewToRightLayout(view, useDefaultAnim);
		setIdTagForMenu(view);
		return view;
	}

	public View addRightTextMenu(Context context, int textId, int paddings, OnClickListener onMenuClickListener) {
		return addRightTextMenu(context, textId, paddings, onMenuClickListener, false);
	}

	public View addRightTextMenu(Context context, int textId, int paddings, OnClickListener onMenuClickListener, boolean useDefaultAnim) {
		Button view = createTextMenu(context, textId, paddings, onMenuClickListener);
		addViewToRightLayout(view, useDefaultAnim);
		return view;
	}

	public Button createTextMenu(Context context, int textId, int paddings, OnClickListener onMenuClickListener) {
		Button button = new Button(context);
		button.setText(textId);
		button.setTextSize(16);
		button.setGravity(Gravity.CENTER);
		button.setTextColor(Color.WHITE);
		button.setBackgroundColor(Color.TRANSPARENT);

		LinearLayout.LayoutParams btnLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		paddings = BaseUtils.dip(context, paddings);
		button.setPadding(paddings, paddings, paddings, paddings);
		button.setLayoutParams(btnLP);

		button.setOnClickListener(onMenuClickListener);
		return button;
	}

	/**
	 * 在标题栏右侧加文字
	 * 
	 * @param context
	 * @param onMenuClickListener
	 * @param text
	 */
	public void addRightText(Activity context, OnClickListener onMenuClickListener, String text) {
		textRight.setVisibility(View.VISIBLE);
		textRight.setOnClickListener(onMenuClickListener);
		textRight.setText(text);
	}

	public View addLeftMenu(Context context, View view, OnClickListener onMenuClickListener) {
		return addLeftMenu(context, view, onMenuClickListener, false);
	}

	public View addLeftMenu(Context context, View view, OnClickListener onMenuClickListener, boolean useDefaultAnim) {
		view.setOnClickListener(onMenuClickListener);
		addViewToLeftLayout(view, useDefaultAnim);
		setIdTagForMenu(view);
		return view;
	}

	public FrameLayout createDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
		FrameLayout layF = (FrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.view_title_part_drawable_menu, null);
		ImageView imageView = (ImageView) layF.findViewById(R.id.imageView);
		imageView.setBackgroundResource(drawableId);
		FrameLayout.LayoutParams btnLP = new FrameLayout.LayoutParams(BaseUtils.dip(context, width), BaseUtils.dip(context, height));
		imageView.setLayoutParams(btnLP);
		btnLP.gravity = Gravity.CENTER_VERTICAL;
		layF.setOnClickListener(onMenuClickListener);
		return layF;
	}

	public void removeAllMenu(boolean useDefaultAnim) {
		removeAllLeftMenu(useDefaultAnim);
		removeAllRightMenu(useDefaultAnim);
	}

	public void removeAllLeftMenu(boolean useDefaultAnim) {
		if (useDefaultAnim) {
			long duration = DEFAULT_ANIM_DURATION;
			layLeft.startAnimation(AnimUtils.init().addAlpha(1f, 0.3f, 0, duration, new LinearInterpolator(), 1).create());
			layRight.postDelayed(new Runnable() {
				@Override
				public void run() {
					layLeft.removeAllViews();
				}
			}, duration);
		}
		layLeft.removeAllViews();
	}

	public void removeAllRightMenu(boolean useDefaultAnim) {
		if (useDefaultAnim) {
			long duration = DEFAULT_ANIM_DURATION;
			layRight.startAnimation(AnimUtils.init().addAlpha(1f, 0.3f, 0, duration, new LinearInterpolator(), 1).create());
			layRight.postDelayed(new Runnable() {
				@Override
				public void run() {
					layRight.removeAllViews();
				}
			}, duration);
		} else {
			layRight.removeAllViews();
		}
	}

	private void addViewToLeftLayout(final View view, boolean useDefaultAnim) {
		if (useDefaultAnim) {
			final long duration = DEFAULT_ANIM_DURATION;
			layLeft.postDelayed(new Runnable() {
				@Override
				public void run() {
					layLeft.addView(view);
					layLeft.startAnimation(AnimUtils.init().addAlpha(0.3f, 1f, 0, duration, new LinearInterpolator(), 1).create());
				}
			}, duration);
		} else {
			layLeft.addView(view);
		}
	}

	private void addViewToRightLayout(final View view, boolean useDefaultAnim) {
		if (useDefaultAnim) {
			final long duration = DEFAULT_ANIM_DURATION;
			layRight.postDelayed(new Runnable() {
				@Override
				public void run() {
					layRight.addView(view);
					layRight.startAnimation(AnimUtils.init().addAlpha(0.3f, 1f, 0, duration, new LinearInterpolator(), 1).create());
				}
			}, duration);
		} else {
			layRight.addView(view);
		}
	}

	private void setIdTagForMenu(View view) {
		view.setId(menuId);
		menuId++;
	}
}
